/************************************************************
	File:		Entity.cpp
	Author:		Mitchel Mishiev, Keisuke Miyazaki
	Purpose:	CEntity class controls the Game Objects
************************************************************/

#include "Entity.h"

#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../Animation System/AnimationSystem.h"

#define TM CSGD_TextureManager::GetInstance()

CEntity::CEntity(void)
{
	m_eType = ENT_DEFAULT;
	m_eDirection = eUp;
	m_fRotation = 0.0f;

	m_fScaleX	= 1.0f;
	m_fScaleY	= 1.0f;

	m_bIsAlive  = true;

	m_unRefCount = 1;
}

// Update
void CEntity::Update( float fElapsedTime )
{
	m_fPositionX += m_fVelocityX * fElapsedTime;
	m_fPositionY += m_fVelocityY * fElapsedTime;
}

// Render
void CEntity::Render( void )
{
	TM->Draw(
		m_nImageID, (int)m_fPositionX, (int)m_fPositionY,
		m_fScaleX, m_fScaleY, nullptr,
		m_fWidth * 0.5f, m_fHeight * 0.5f, m_fRotation );
}

// CheckCollision
bool CEntity::CheckCollision( const IEntity* pEntity1, const IEntity* pEntity2  )
{
	RECT rOverlap = { };
	RECT rEntity1 = pEntity1->GetRect();
	RECT rEntity2 = pEntity2->GetRect();

	RECT rHitBox  = CAnimationSystem::GetInstance()->GetRect();

	if( IntersectRect( &rOverlap, &rEntity1, &rEntity2 ) == FALSE 
		&& IntersectRect(&rOverlap, &rHitBox, &rEntity2 ) == FALSE )
		return false;

	HandleCollision( pEntity2 );

	return true;
}

// Release
void CEntity::Release( void )
{
	--m_unRefCount;

	if( m_unRefCount == 0 )
		delete this;
}

RECT CEntity::GetRect( void ) const 
{
	RECT rRect;

	rRect.left		= (LONG)m_fPositionX;
	rRect.top		= (LONG)m_fPositionY;
	rRect.right		= (LONG)(m_fPositionX + m_fWidth);
	rRect.bottom	= (LONG)(m_fPositionY + m_fHeight);

	return rRect;
}

